package models;

import play.*;
import play.data.validation.Required;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;

@Entity
public class ActionLog extends Model {
	@Required
	public String type;
	@Required
	@ManyToOne
	public User owner;
	@OneToOne
	public Comment relatedComment;

	/**
	 * 
	 * @param type
	 *            the type of the action log (e.g: a comment, a new idea,
	 *            ...etc)
	 * @param owner
	 *            the user who triggered the log message
	 * @param relatedComment
	 *            if the type was a comment, this is the comment itself
	 */
	public ActionLog(String type, User owner, Comment relatedComment) {
		this.type = type;
		this.owner = owner;
		this.relatedComment = relatedComment;
	}
	/**
	 * @author Ahmed Mahmoud
	 * @return
	 * 
	 */
	public boolean cascadeDelete(){
		this.owner.actions.remove(this);
		owner.save();
		this.delete();
		return true;
	}
}
